/**
 * 
 */
package com.chuantou.yuba.provider;

import java.util.ArrayList;

import android.content.ContentProvider;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.OperationApplicationException;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

import com.chuantou.yuba.db.DBHelper;
import com.chuantou.yuba.db.columns.CacheColumns;
import com.chuantou.yuba.db.columns.CoverColumns;
import com.chuantou.yuba.db.columns.MessageColumns;

/**
 * @description 提供本地数据库访问
 */
public class DatabaseProvider extends ContentProvider {

	public static final String AUTHORITY = "com.chuantou.yuba.databases";

	private static final int CODE_CACHE = 1;
	private static final int CODE_MESSAGE = 2;
	private static final int CODE_COVER = 3;

	private static final String CONTENT_TYPE_CACHE = "vnd.android.cursor.dir/vnd.com.chuantou.yuba.cache";
	private static final String CONTENT_TYPE_MESSAGE = "vnd.android.cursor.dir/vnd.com.chuantou.yuba.message";
	private static final String CONTENT_TYPE_COVER = "vnd.android.cursor.dir/vnd.com.chuantou.yuba.cover";

	private DBHelper mDBHelper = null;

	public static final String TAG = DatabaseProvider.class.getName();

	private static final UriMatcher sUriMatcher;
	static {
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(AUTHORITY, CacheColumns.TABLE_NAME, CODE_CACHE);
		sUriMatcher.addURI(AUTHORITY, MessageColumns.TABLE_NAME, CODE_MESSAGE);
		sUriMatcher.addURI(AUTHORITY, CoverColumns.TABLE_NAME, CODE_COVER);
	}

	@Override
	public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
		SQLiteDatabase db = mDBHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			ContentProviderResult[] results = super.applyBatch(operations);
			db.setTransactionSuccessful();
			return results;
		} finally {
			db.endTransaction();
		}
	}

	@Override
	public int delete(Uri uri, final String selection, String[] selectionArgs) {
		SQLiteDatabase db = mDBHelper.getWritableDatabase();
		int count;
		int match = sUriMatcher.match(uri);
		switch (match) {
		case CODE_CACHE:
			count = db.delete(CacheColumns.TABLE_NAME, selection, selectionArgs);
			break;
		case CODE_MESSAGE:
			count = db.delete(MessageColumns.TABLE_NAME, selection, selectionArgs);
			break;
		case CODE_COVER:
			count = db.delete(CoverColumns.TABLE_NAME, selection, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		return count;

	}

	@Override
	public String getType(Uri uri) {
		switch (sUriMatcher.match(uri)) {
		case CODE_CACHE:
			return CONTENT_TYPE_CACHE;
		case CODE_MESSAGE:
			return CONTENT_TYPE_MESSAGE;
		case CODE_COVER:
			return CONTENT_TYPE_COVER;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues initialValues) {
		ContentValues values;
		if (initialValues != null) {
			values = new ContentValues(initialValues);
		} else {
			values = new ContentValues();
		}
		SQLiteDatabase db = mDBHelper.getWritableDatabase();
		long rowId = 0;
		Uri insertedUri = null;
		switch (sUriMatcher.match(uri)) {
		case CODE_CACHE:
			rowId = db.insert(CacheColumns.TABLE_NAME, null, values);
			insertedUri = CacheColumns.CONTENT_URI;
			break;
		case CODE_MESSAGE:
			rowId = db.insert(MessageColumns.TABLE_NAME, null, values);
			insertedUri = MessageColumns.CONTENT_URI;
			break;
		case CODE_COVER:
			rowId = db.insert(CoverColumns.TABLE_NAME, null, values);
			insertedUri = CoverColumns.CONTENT_URI;
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		if (rowId > 0) {
			Uri cacheUri = ContentUris.withAppendedId(insertedUri, rowId);
			return cacheUri;
		}
		return null;
	}

	@Override
	public boolean onCreate() {
		mDBHelper = new DBHelper(getContext());
		SQLiteDatabase db = mDBHelper.getReadableDatabase();
		return db != null;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		int queryCode = sUriMatcher.match(uri);
		switch (queryCode) {
		case CODE_CACHE:
			qb.setTables(CacheColumns.TABLE_NAME);
			break;
		case CODE_MESSAGE:
			qb.setTables(MessageColumns.TABLE_NAME);
			break;
		case CODE_COVER:
			qb.setTables(CoverColumns.TABLE_NAME);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		SQLiteDatabase db = mDBHelper.getReadableDatabase();
		Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
		c.setNotificationUri(getContext().getContentResolver(), uri);
		return c;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		int match = sUriMatcher.match(uri);
		SQLiteDatabase db = mDBHelper.getWritableDatabase();
		int count = 0;
		switch (match) {
		case CODE_CACHE:
			count = db.update(CacheColumns.TABLE_NAME, values, selection, selectionArgs);
			break;
		case CODE_MESSAGE:
			count = db.update(MessageColumns.TABLE_NAME, values, selection, selectionArgs);
			break;
		case CODE_COVER:
			count = db.update(CoverColumns.TABLE_NAME, values, selection, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		return count;
	}

}
